package com.meiqiongproperty.yiguanjia.fragment.order.orderAdapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.databinding.OrderDetailListItemBinding;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.OnClick;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.OnItemClickListener;
import com.meiqiongproperty.yiguanjia.httpbean.order.OrderDetailListBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/30.
 */

public class OrderDetailListAdapter extends RecyclerView.Adapter<OrderDetailListAdapter.Holder> {
    Context context;
    private OnItemClickListener mOnItemClickListener;
    private OnClick onClick;

    List<OrderDetailListBean.OrderdetailBean.ShopOrderInfoBean> list=new ArrayList<>();
    LayoutInflater inflater;
    OrderDetailListItemBinding binding;
    OrderDetailListCenterAdapter adapter;
    String status;


    public OrderDetailListAdapter(Context context, List<OrderDetailListBean.OrderdetailBean.ShopOrderInfoBean> list) {
        this.context = context;
        this.list=list;
        inflater=LayoutInflater.from(context);
    }

    public void setStatus(String status){
        this.status=status;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        binding= DataBindingUtil.inflate(inflater, R.layout.order_detail_list_item,parent,false);
        Holder holder=new Holder(binding.getRoot());
        holder.setBinding(binding);
        return holder;
    }

    @Override
    public void onBindViewHolder(final Holder holder, final int position) {
        adapter=new OrderDetailListCenterAdapter(context,list.get(position).getSubOrderDetail());
        binding.recyclerOrderDetail.setAdapter(adapter);

        OrderDetailListBean.OrderdetailBean.ShopOrderInfoBean bean=list.get(position);
        if (bean.getOrderStatus().equals("1")||bean.getOrderStatus().equals("2")||bean.getOrderStatus().equals("4")||bean.getOrderStatus().equals("5")){
            holder.binding.btnConfirm.setVisibility(View.GONE);
        }else if(bean.getOrderStatus().equals("3")){
            holder.binding.btnConfirm.setVisibility(View.VISIBLE);
        }
        binding.orderProductTotalPrice.setText("￥"+bean.getOrderPrice());

       if (mOnItemClickListener != null) {
            /**
             * 这里加了判断，itemViewHolder.itemView.hasOnClickListeners()
             * 目的是减少对象的创建，如果已经为view设置了click监听事件,就不用重复设置了
             * 不然每次调用onBindViewHolder方法，都会创建一个监听事件对象，增加了内存的开销
             */
            if (!holder.itemView.hasOnClickListeners()) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getPosition();
                        mOnItemClickListener.onItemClick(v, position);
                    }
                });
            }
        }

        if (onClick != null) {
            holder.binding.btnConfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClick.onClick(v,position);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class Holder extends RecyclerView.ViewHolder {
        OrderDetailListItemBinding binding;

        public OrderDetailListItemBinding getBinding() {
            return binding;
        }

        public void setBinding(OrderDetailListItemBinding binding) {
            this.binding = binding;
        }

        public Holder(View itemView) {
            super(itemView);
        }
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public void setOnClickListener(OnClick onClick){
        this.onClick=onClick;
    }
}
